package com.itany.deepseek.controller;

import com.itany.deepseek.entity.User;
import com.itany.deepseek.exception.DuplicateUsernameException;
import com.itany.deepseek.exception.UserDisabledException;
import com.itany.deepseek.exception.UserNotFoundException;
import com.itany.deepseek.service.UserService;
import com.itany.deepseek.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @ExceptionHandler(DuplicateUsernameException.class)
    public ResponseResult registExceptionHandler(Exception e){
        return new ResponseResult("500",e.getMessage(),null);
    }

    @ExceptionHandler({UserNotFoundException.class,UserDisabledException.class})
    public ResponseResult loginExceptionHandler(Exception e){
        return new ResponseResult("500",e.getMessage(),null);
    }

    @RequestMapping("/regist")
    public ResponseResult regist(User user) throws DuplicateUsernameException {
        userService.regist(user);
        return ResponseResult.success("注册成功");
    }

    @RequestMapping("/login")
    public ResponseResult login(String username,String password) throws UserNotFoundException, UserDisabledException {
        User user=userService.login(username,password);
        return ResponseResult.success("登录成功",user);
    }


}
